package com.wryyyyy.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wryyyyy.dto.AddLabelDTO;
import com.wryyyyy.dto.AddMaterialDTO;
import com.wryyyyy.entity.DishLabel;
import com.wryyyyy.entity.DishMaterial;
import com.wryyyyy.entity.Label;
import com.wryyyyy.mapper.DishLabelMapper;
import com.wryyyyy.service.IDishLabelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wryyyyy.utils.SnowFlakeCompone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wryyyyy
 * @since 2023-11-12
 */
@Service
public class DishLabelServiceImpl extends ServiceImpl<DishLabelMapper, DishLabel> implements IDishLabelService {
    @Resource
    private DishLabelMapper dishLabelMapper;

    @Autowired
    SnowFlakeCompone snowFlakeCompone;

    @Override
    public Integer addDishLabel(String dishId, List<AddLabelDTO> records) {
        if (records.isEmpty()) {
            return 0;
        }

        // 删除之前的
        List<DishLabel> oldRecords = dishLabelMapper.selectByDish(dishId);
        List<String> oldRecordIds = new ArrayList<>();
        for (DishLabel record : oldRecords) {
            oldRecordIds.add(record.getId());
        }
        if(!oldRecordIds.isEmpty()) {
            dishLabelMapper.deleteBatchIds(oldRecordIds);
        }

        // 新增
        for (AddLabelDTO record : records) {
            record.setAddId(snowFlakeCompone.getInstance().nextId()+"");
        }
        return dishLabelMapper.insertBatch(dishId, records);
    }
}
